//
//  KRShareWebviewController.m
//  cookBook
//
//  Created by kuroken on 16/2/3.
//  Copyright © 2016年 kuroken. All rights reserved.
//
#define MAS_SHORTHAND

#import "KRShareWebviewController.h"
#import <MJExtension.h>
#import <Masonry.h>
#import <SVProgressHUD.h>
#import "KRConst.h"
#import "KRHttpTool.h"
#import "KRReadPost.h"
#import "KRCateTagController.h"
#import "KRRecipeInfoController.h"
#import "KRReadPostController.h"

@interface KRShareWebviewController () <UIWebViewDelegate>
/** KRReadPost模型 */
@property (nonatomic, strong) KRReadPost *readPost;
/** UIWebView */
@property (nonatomic, weak) UIWebView *webView;
@end

@implementation KRShareWebviewController

- (UIWebView *)webView
{
    if (!_webView) {
        UIWebView *webView = [[UIWebView alloc] init];
        webView.delegate = self;
        webView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:webView];
        [webView makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.left.and.right.and.bottom.equalTo(self.view);
        }];
        _webView = webView;
    }
    return _webView;
}

- (void)dealloc
{
    NSLog(@"%s", __func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self loadData];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!_webView) {
        [SVProgressHUD show];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    NSString *url = @"http://api.hoto.cn/index.php?appid=4&format=json&method=WebView.initWebView&v=2&vc=40&vn=v5.1.0";

    [KRHttpTool post:url params:@{@"id" : self.shareID} success:^(NSURLSessionDataTask *task, id json) {
        self.readPost = [KRReadPost mj_objectWithKeyValues:json[@"result"]];
        
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.readPost.shareUrl]]];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        KRLog(@"%@, %@", self, error);
    }];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([[request.URL description] isEqualToString:self.readPost.shareUrl]) {
        return YES;
    }
    
    NSString *uselessStr = [request.URL description];
    if ([uselessStr containsPartString:@"baidu"] || [uselessStr containsPartString:@"alicdn"] || [uselessStr containsPartString:@"tanx"]) {
//        KRLog(@"%@", uselessStr);
        return NO;
    }

    // 页面跳转
    NSString *jumpUrlStr = [request.URL description];
    KRLog(@"%@", jumpUrlStr);
    if ([jumpUrlStr containsPartString:@"recipe"]) {
//        NSLog(@"%@", jumpUrlStr);
        if ([jumpUrlStr containsPartString:@"all"]) { // 分类标签
            KRCateTagController *cateTagVc = [[KRCateTagController alloc] init];
            
            NSRange range = [jumpUrlStr rangeOfString:@"all/"];
            NSUInteger loc = range.location + range.length;
            NSRange range2 = [jumpUrlStr rangeOfString:@"?src"];
            NSUInteger len = range2.location - loc;
            NSString *tagID = [jumpUrlStr substringWithRange:NSMakeRange(loc, len)];
            cateTagVc.tagID = tagID;
            
            if ([jumpUrlStr containsPartString:@"nyf"]) {
                cateTagVc.title = @"年夜饭";
            }
            [self.navigationController pushViewController:cateTagVc animated:YES];
        } else { // 单条食谱
            if ([jumpUrlStr containsPartString:@"photo"]) return NO;
            KRRecipeInfoController *infoVc = [[KRRecipeInfoController alloc] init];
            
            NSRange range = [jumpUrlStr rangeOfString:@"recipe/"];
            NSUInteger loc = range.location + range.length;
            NSRange range2 = [jumpUrlStr rangeOfString:@"?src"];
            NSString *recipID;
            if (range2.length) {
                NSUInteger len = range2.location - loc;
                recipID = [jumpUrlStr substringWithRange:NSMakeRange(loc, len)];
            } else {
                recipID = [jumpUrlStr substringFromIndex:loc];
            }
            
            infoVc.recipeID = recipID; // RecipeId
            [self.navigationController pushViewController:infoVc animated:YES];
        }
    } else if ([jumpUrlStr containsPartString:@"topic"]) { // 单条阅读
        KRReadPostController *readPostVc = [[KRReadPostController alloc] init];
        
        NSRange range = [jumpUrlStr rangeOfString:@"topic-"];
        NSUInteger loc = range.location + range.length;
        NSRange range2 = [jumpUrlStr rangeOfString:@".html"];
        NSUInteger len = range2.location - loc;
        NSString *topicID = [jumpUrlStr substringWithRange:NSMakeRange(loc, len)];
        readPostVc.topicID = topicID;
        [self.navigationController pushViewController:readPostVc animated:YES];
    }
    
    return NO;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [SVProgressHUD dismiss];
}

@end